package com.lanou.moving.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import com.lanou.moving.R;
import com.lanou.moving.activity.SingerDetailActivity;
import com.lanou.moving.adapter.SingerInfoAdapter;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.entity.SingerInfo;
import com.lanou.moving.entity.SingerSimilar;
import com.lanou.moving.net.HttpHelper;
import com.lanou.moving.net.NetDataCallBack;
import com.lanou.moving.view.MyGridView;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 15/10/24.
 */
public class SingerInfoFragment extends BaseFragment implements NetDataCallBack, AdapterView.OnItemClickListener {

    @Bind(R.id.fragment_singer_similar)
    MyGridView fragmentSingerSimilar;
    @Bind(R.id.singer_info)
    TextView singerInfo;

    private int singer_id;
    private SingerInfo.DataEntity info;
    private List<SingerSimilar.DataEntity> singers;
    private HttpHelper httpHelper;
    private SingerInfoAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_singer_info, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        singer_id = bundle.getInt("singer_id");
        httpHelper = new HttpHelper(this);
        httpHelper.getSingerSimilar(singer_id);
        httpHelper.getSingerInfo(singer_id);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void netError(int what) {
        Toast.makeText(mContext, "网络数据拉取错误+" + what, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void netSuccess(int what, Object object) {
        switch (what) {
            case WHAT_SINGER_SIMILAR://接口1返回的数据
                singers = ((SingerSimilar)object).getData();
                adapter = new SingerInfoAdapter(mContext,singers);
                fragmentSingerSimilar.setAdapter(adapter);
                fragmentSingerSimilar.setOnItemClickListener(this);
                break;
            case WHAT_SINGER_INFO://接口2返回的数据
                info = ((SingerInfo)object).getData();
                singerInfo.setText(info.getBrief());
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        singer_id = singers.get(position).getSingerId();
        Intent intent = new Intent(getActivity(), SingerDetailActivity.class);
        intent.putExtra("singer_id", singer_id);
        startActivity(intent);
    }
}
